﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.nickro.project.sound{
    import flash.events.*;
    import flash.media.*;
    import com.tobyfreeman.utils.*;
    import br.com.stimuli.loading.*;

    public class SoundsGroup extends EventDispatcher {

        private var pID:String;
        private var pChanTable:Object;
        private var pDispatchProgressEvents:Boolean;
        private var pFolderURL:String;
        private var pBulkLoader:BulkLoader;
        private var pSoundDefLookup:Object;
        private var pSoundsObject:Sounds;
        private var pName:String;

        public static const PATH_SEPARATOR:String = "/";

        public function SoundsGroup(name:String){
            super();
            pName = name;
            pFolderURL = "";
            pChanTable = new Object();
        }
        public function getName():String{
            return (pName);
        }
        public function dispatchProgressEvents(dispatchFlag:Boolean):void{
            pDispatchProgressEvents = dispatchFlag;
        }
        public function getChan(name:String):SoundChan{
            if (((!(pChanTable.hasOwnProperty(name))) || ((pChanTable[name].length == 0)))){
                return (createChan(name));
            };
            return (pChanTable[name][0]);
        }
        protected function loadCompleteHandler(e:Event):void{
            dispatchEvent(e);
        }
        protected function loadProgressHandler(e:BulkProgressEvent):void{
            if (pDispatchProgressEvents){
                dispatchEvent(e);
            };
        }
        public function setExternalFolder(folder:String, trailingSlash:Boolean=true):void{
            pFolderURL = folder;
            if (((((!(Strings.isWhiteSpace(folder))) && (trailingSlash))) && (!(Strings.endsWith(pFolderURL, PATH_SEPARATOR))))){
                pFolderURL = (pFolderURL + PATH_SEPARATOR);
            };
        }
        protected function setSoundsObject(oSounds:Sounds):void{
            pSoundsObject = oSounds;
            pID = ((oSounds.getID() + "_") + pName);
        }
        protected function loadErrorHandler(e:BulkErrorEvent):void{
            dispatchEvent(e);
        }
        public function getChans(name:String):Array{
            if (!pChanTable.hasOwnProperty(name)){
                pChanTable[name] = [];
            };
            return (pChanTable[name]);
        }
        override public function toString():String{
            return ((((("[object SoundGroup ID='" + pID) + "' name='") + pName) + "']"));
        }
        public function createChan(name:String):SoundChan{
            var oSound:Sound = pBulkLoader.getSound(name);
            var oSoundDef:SoundDef = pSoundDefLookup[name];
            if (!(oSoundDef is SoundDef)){
                throw (new Error((("/error/ No SoundDef in SoundsGroup.createChan('" + name) + "')")));
            };
            var oChan:SoundChan = new SoundChan(oSound, oSoundDef);
            if (!pChanTable.hasOwnProperty(name)){
                pChanTable[name] = [];
            };
            pChanTable[name].push(oChan);
            return (oChan);
        }
        function createFromXML(oXML:XML):void{
            var oSoundDef:SoundDef;
            var oNode:XML;
            pSoundDefLookup = new Object();
            var oSoundNodes:XMLList = oXML..sound;
            for each (oNode in oSoundNodes) {
                oSoundDef = new SoundDef(oNode.@name, oNode.@volume, oNode.@pan, oNode.@crossover);
                pSoundDefLookup[oSoundDef.pName] = oSoundDef;
            };
        }
        public function stopChan(name:String):void{
            if (((!(pChanTable.hasOwnProperty(name))) || ((pChanTable[name].length == 0)))){
                return;
            };
            pChanTable[name][0].stop();
        }
        public function getID():String{
            return (pID);
        }
        function startLoading(oSounds:Sounds):void{
            var oSoundDef:SoundDef;
            var oParams:Object;
            setSoundsObject(oSounds);
            if (!(pBulkLoader is BulkLoader)){
                pBulkLoader = BulkLoader.getLoader(pID);
                if ((pBulkLoader is BulkLoader)){
                    pBulkLoader.removeAll();
                } else {
                    pBulkLoader = new BulkLoader(pID);
                };
                pBulkLoader.addEventListener(BulkLoader.ERROR, loadErrorHandler, false, 0, true);
                pBulkLoader.addEventListener(BulkLoader.COMPLETE, loadCompleteHandler, false, 0, true);
                if (pDispatchProgressEvents){
                    pBulkLoader.addEventListener(BulkLoader.PROGRESS, loadProgressHandler, false, 0, true);
                };
            } else {
                trace(("/note/ RELOADING SOUND GROUP " + this));
                pBulkLoader.removeAll();
            };
            for each (oSoundDef in pSoundDefLookup) {
                oParams = {id:oSoundDef.pName, type:"sound", maxRetries:oSounds.pMaxRetries};
                pBulkLoader.add((pFolderURL + oSoundDef.pFilename), oParams);
            };
            pBulkLoader.start();
        }

    }
}//package com.tobyfreeman.sound 
